// pages/game/game.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sum: 0,
    player: './image/wenhao.gif',
    computer: './image/st.png',
    tips: '',
    outInter:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var timestart=0;
    var timestep=1;
    const computerArr = ['./image/st.png','./image/jd.png','./image/bu.png'];
    this.outInter= setInterval(()=>{
      timestart+=timestep  
      if(timestart>2){
        timestart=0
      }
      const computerImg = computerArr[timestart];  
      this.setData({
        computer:computerImg
      })
      // console.log(this.setData)
      // console.log(computerArr[timestart])
    },100)
  },
  xuanze: function (e) {
    // console.log(e)
    if(this.data.player==='./image/wenhao.gif'){
      const xuanze = e.target.dataset.xuanze;
    const st = './image/st.png';
    const jd = './image/jd.png';
    const bu = './image/bu.png';
    clearInterval(this.outInter)         //停止定时器
    if (xuanze === '0') {
      this.setData({
        player: st
      })
    } else if (xuanze === '1') {
      this.setData({
        player: jd
      })
    } else {
      this.setData({
        player: bu
      })
    }
    // console.log(this.data.player)
    const player = this.data.player;
    const computer = this.data.computer;
    if ((player === st && computer === jd) || (player === jd && computer === bu) || (player === bu && computer === st)) {
      this.setData({
        tips: '获胜了',
        sum: this.data.sum+1
      })
    } else if (player === computer) {
      this.setData({
        tips: '平局了'
      })
    } else {
      this.setData({
        tips: '失败了'
      })
    }
    }
    
  },

  btn: function () {
    if(this.data.player!=='./image/wenhao.gif'){
      this.setData({
      player: './image/wenhao.gif',
      tips: ''
    })
    var timestart=0;
    var timestep=1;
    const computerArr = ['./image/st.png','./image/jd.png','./image/bu.png'];
    this.outInter= setInterval(()=>{
      timestart+=timestep  
      if(timestart>2){
        timestart=0
      }
      const computerImg = computerArr[timestart];  
      this.setData({
        computer:computerImg
      })
    },100)
    }
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})